package com.speed.util.html;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class GenerStaticHTML {

	public static void create(TemplateData td) {
		try {
			// 创建一个合适的Configration对象
			Configuration configuration = new Configuration();
			configuration.setDirectoryForTemplateLoading(new File(td.getInPath()));
			configuration.setObjectWrapper(new DefaultObjectWrapper());
			// 这个一定要设置，不然在生成的页面中 会乱码
			configuration.setDefaultEncoding("UTF-8"); 
			// 获取或创建一个模版。
			Template template = configuration.getTemplate(td.getTempName());
			Map<String, Object> paramMap = td.getDatas();

			Writer writer = new OutputStreamWriter(new FileOutputStream(td.getOutPath()), "UTF-8");
			template.process(paramMap, writer);
			System.out.println("静态页面生成完成!~~");
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		}
	}

	@Test
	public void testCreateHTML() throws Exception {
		try {
			// 创建一个合适的Configration对象
			Configuration configuration = new Configuration();
			configuration.setDirectoryForTemplateLoading(new File("E:\\test\\"));
			configuration.setObjectWrapper(new DefaultObjectWrapper());
			configuration.setDefaultEncoding("UTF-8"); // 这个一定要设置，不然在生成的页面中 会乱码
			// 获取或创建一个模版。
			Template template = configuration.getTemplate("static.html");
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("name", "周炽坚");

			Writer writer = new OutputStreamWriter(new FileOutputStream("E:\\test\\success.html"), "UTF-8");
			template.process(paramMap, writer);

			System.out.println("恭喜，生成成功~~");
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		}

	}

}
